using System.Collections;
using UnityEngine;

public class MissionTargetHijackVehicle : MissionTargetBase
{
	[SerializeField]
	protected string vehicleName;

	[SerializeField]
	protected bool privateCar;

	[SerializeField]
	protected Vector3 vehiclePosition;

	[SerializeField]
	protected Vector3 vehicleRotation;

	protected EntityBehavior loadedCar;

	protected PointerPanel pointer;

	protected bool carLoaded;

	protected override bool OnStartTarget()
	{
		if (!base.OnStartTarget())
		{
			return false;
		}
		StartCoroutine(LoadCar());
		return true;
	}

	private IEnumerator LoadCar()
	{
		carLoaded = false;
		if (loadedCar == null)
		{
			ResourceRequest carRequest = Resources.LoadAsync("Cars/" + ((!privateCar) ? string.Empty : "PrivateCars/") + vehicleName);
			yield return carRequest;
			loadedCar = ((GameObject)Object.Instantiate(carRequest.asset, vehiclePosition, Quaternion.Euler(vehicleRotation))).GetComponent<EntityBehavior>();
			loadedCar.initialPosition = vehiclePosition;
			loadedCar.initialRotation = Quaternion.Euler(vehicleRotation);
			if (privateCar)
			{
				loadedCar.privateCar = false;
			}
			loadedCar.photonView.viewID = PhotonNetwork.AllocateViewID();
		}
		else
		{
			if (GameController.instance.playerScript.currentCar == loadedCar)
			{
				GameController.instance.playerScript.GetOutOfCar(false);
			}
			loadedCar.transform.position = vehiclePosition;
			loadedCar.transform.rotation = Quaternion.Euler(vehicleRotation);
		}
		pointer = PointerController.instance.AddPointer(loadedCar.transform.position, 0f);
		carLoaded = true;
	}

	public override void OnUpdateTarget()
	{
		if (carLoaded && GameController.instance.playerScript.currentCar == loadedCar)
		{
			CompleteTarget();
		}
	}

	public override void OnStopTarget()
	{
		base.OnStopTarget();
		if (pointer != null)
		{
			PointerController.instance.RemovePointer(pointer);
		}
	}

	protected override bool TargetIsComplete()
	{
		return false;
	}
}
